package com.example.narratejpa.controller;

import com.example.narratejpa.entiy.Region;
import com.example.narratejpa.repository.RegionRepository;
import com.example.narratejpa.service.RegionService;
import com.sun.deploy.net.HttpRequest;
import com.sun.deploy.net.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties;
import org.springframework.data.domain.Page;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.jws.WebParam;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
public class RegionController {
   @Autowired
   private RegionService regionService;
    //查詢列表
    @GetMapping("/list")
    public Page<Region> getList(@RequestParam(value = "page") Integer page,
                                @RequestParam(value = "size") Integer size){

        System.out.println("分页： page:"+page+"; size:"+size);
        return regionService.getPage(page, size);
    }
    //添加
    @PostMapping(value = "/add")
    public String addRegion(Region region){
        //獲取時間
        int time = (int) (System.currentTimeMillis()/1000);
        region.setGmt_create(time);
        try{
        Region region1 = regionService.addRegion(region);
            if (region1!=null){
                return  "success";
            }else{
                return "fail";
            }
        }catch (Exception e){
            return "fail";
        }
    }
    //删除
    @GetMapping("/del")
    public String delRegion(Integer id){
        try{
         regionService.delRegionByID(id);
         return "success";
        }catch (Exception e){
            return "fail";
        }
    }
    //根据id查询
    @GetMapping("/findRegionById")
    public Region findRegionById(Integer id){
        Region region = regionService.findRegionByID(id);
        return region;
    }
    //修改
    @PostMapping(value = "/update")
    public String updateRegion(Region region){
        try{
            regionService.updateRegion(region);
            return "success";
        }catch (Exception e){
            return  "fail";
        }
    }

    //批量删除
    @GetMapping(value = "/batchDeletRegion")
    public  String batchDeletRegion(String ids){
        String[] list = ids.split(";");
        List<Integer> LString = new ArrayList<Integer>();
        for(String str :list ){
            LString.add(new Integer(str));
        }

//        for (String s:stuList){
//            System.out.println(s);
//        }
//        System.out.println(stuList.length);
        try{
            regionService.batchDelectRegion(LString);
            return "success";
        }catch (Exception e){
            return "fail";
        }
    }

    @PostMapping("/test")
    @ResponseBody
    public String test(@RequestParam(value="name") String name){
        System.out.println(name);
       return  name;
    }
}
